package com.shens.blog.ui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.*;
import javax.swing.text.DefaultCaret;

import com.shens.blog.service.InitInterface;
import com.shens.blog.service.OnBtnClickListener;

public class JOutputPanel extends JPanel implements InitInterface {
    private static final long serialVersionUID = 1L;
    JPanel panel1 = null;
    JPanel panel2 = null;
    JButton action = null;
    JButton clear = null;
    JButton clearOut = null;
    JButton exitThread = null;
    JTextArea field = null;
    JTextArea textAout = null;
    public OnBtnClickListener clickListener;

    public JOutputPanel() {
    }

    public void initView() {
        this.setLayout(new GridLayout(1, 2));
        this.setSize(850, 500);
        this.panel1 = new JPanel();
        this.panel2 = new JPanel();
        this.panel1.setBackground(Color.LIGHT_GRAY);
        this.panel2.setBackground(Color.white);
        this.add(this.panel1, "West");
        this.add(this.panel2, "East");
        this.initPanel1();
        this.initPanel2();
        this.initListener();
    }

    private void initPanel2() {
        JLabel label = new JLabel("system print:");
        label.setPreferredSize(new Dimension(this.getWidth(), 80));
        this.panel2.setLayout(new FlowLayout(0));
        this.panel2.setAlignmentY(0.5F);
        this.textAout = new JTextArea();
        this.textAout.setLineWrap(true);
        this.textAout.setOpaque(false);
        this.textAout.setWrapStyleWord(true);
        this.textAout.setPreferredSize(new Dimension(400, 350));
        this.textAout.setEditable(false);
        DefaultCaret caret = (DefaultCaret)this.textAout.getCaret();
        caret.setUpdatePolicy(2);
        this.clearOut = new JButton("清空");
        this.clearOut.setPreferredSize(new Dimension(200, 40));
        this.exitThread = new JButton("中止");
        this.exitThread.setPreferredSize(new Dimension(200, 40));
        this.panel2.add(label);
        this.panel2.add(new JScrollPane(this.textAout));
        this.panel2.add(this.clearOut);
        this.panel2.add(this.exitThread);
    }

    private void initPanel1() {
        JLabel label = new JLabel("文章地址列表:");
        label.setPreferredSize(new Dimension(this.getWidth(), 80));
        this.panel1.setLayout(new FlowLayout(0));
        this.panel1.setAlignmentY(0.5F);
        this.field = new JTextArea();
        this.field.setLineWrap(true);
        this.field.setOpaque(false);
        this.field.setWrapStyleWord(true);
        this.field.setPreferredSize(new Dimension(400, 350));
        this.field.setText(
            "https://gitee.com/shensong10/shens-blog");
        this.field.setEditable(false);
        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout(0));
        this.action = new JButton("访问");
        this.action.setPreferredSize(new Dimension(190, 40));
        this.clear = new JButton("清空");
        this.clear.setPreferredSize(new Dimension(190, 40));
        panel.setOpaque(false);
        panel.add(this.action);
        panel.add(this.clear);
        this.panel1.add(label);
        this.panel1.add(this.field);
        this.panel1.add(panel);
    }

    private void initListener() {
        this.action.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if (JOutputPanel.this.clickListener != null) {
                    JOutputPanel.this.clickListener.onBtnClick("actionBtn", JOutputPanel.this.field.getText());
                }

            }
        });
        this.clear.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                JOutputPanel.this.field.setText(
                    "https://gitee.com/shensong10/shens-blog");
                JOutputPanel.this.field.setEnabled(false);
            }
        });
        this.clearOut.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                JOutputPanel.this.textAout.setText("");
                JOutputPanel.this.textAout.setEnabled(false);
            }
        });
        this.exitThread.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if (JOutputPanel.this.clickListener != null) {
                    JOutputPanel.this.clickListener.onBtnClick("exitBtn", JOutputPanel.this.field.getText());
                }

            }
        });
    }

    public void appentOutPrint(String appStr) {
        this.textAout.append("syso:" + appStr + "\r\n");
    }

    public void setOnBtnClickListener(OnBtnClickListener listener) {
        this.clickListener = listener;
    }

    public void appArticleUrlList(String url) {
        if (this.field.getText().equals(
            "https://gitee.com/shensong10/shens-blog")) {
            this.field.setText("");
        }

        this.field.append(url + ",\r\n");
    }

    public ArrayList<String> getArticleUrlList() {
        String articleStr = this.field.getText();
        String[] articles = articleStr.split(",\r\n");
        ArrayList<String> strings = new ArrayList();

        for (int i = 0; i < articles.length; ++i) {
            if (!articles[i].equals(
                "https://gitee.com/shensong10/shens-blog")
                && articles[i].matches("[a-zA-z]+://[^\\s]*")) {
                strings.add(articles[i]);
            } else {
                System.out.println(articles[i] + "------");
            }
        }

        return strings.size() == 0 ? null : strings;
    }
}
